package com.shibaobei.activity.controller;

import com.alibaba.fastjson.JSONObject;
import com.shibaobei.activity.service.ProcessDefinitionService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2020/4/29 0029.
 */
@Controller
@RequestMapping("/processDefinition")
public class ProcessDefinitionController {
    @Autowired
    private ProcessDefinitionService processDefinitionService;

    /**
     *获取所有最新版本的流程定义
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        List<ProcessDefinition> lastList = processDefinitionService.findLastList();
        model.addAttribute("list",lastList);
        return "Flow_ProcessDefinition/list";
    }

    /**
     * 根据key删除流程定义
     * @param request
     * @return
     */
    @RequestMapping("/deleteByKey")
    @ResponseBody
    public JSONObject deleteByKey(HttpServletRequest request){
        String key = request.getParameter("key");
        processDefinitionService.deleteByKey(key);
        JSONObject json = new JSONObject();
        json.put("success",true);
        return json;
    }
    @RequestMapping("/deployUI")
    public String deployUI(){
        return "Flow_ProcessDefinition/deployUI";
    }

    /**
     * 部署流程定义
     * @return
     */
    @RequestMapping("/deploy")
    public ModelAndView deploy(MultipartFile resource){

        processDefinitionService.deploy( resource);

        ModelAndView mv = new ModelAndView();
        //使用重定向，此时springmvc.xml配置文件中的视图解析器将会失效
        mv.setViewName("redirect:/processDefinition/list");
        return mv;

    }

    @RequestMapping("/showPng")
    @ResponseBody
    public byte[] showPng(String processDefinitionId) throws IOException {
        InputStream inputStream = processDefinitionService.showPng(processDefinitionId);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }

}
